<?php
class CategoriesController extends BaseAdminController {

	public function actionView($id) {
		$this->render('view', array(
			'model' => $this->loadModel($id, 'Categories'),
		));
	}

	public function actionCreate() {
		$model = new Categories;


		if (isset($_POST['Categories'])) {
			$model->attributes = $_POST['Categories'];
			
			$model->created_by 	= Yii::app()->user->id;
			$model->modified_by = Yii::app()->user->id;
			if($_POST['Categories']['created']=='0000-00-00 00:00:00'){
				$model->created = date('Y-m-d H:i:s');
			}
			if($_POST['Categories']['modified']=='0000-00-00 00:00:00'){
				$model->modified = date('Y-m-d H:i:s');
			}
			
			if ($model->save()) {
				Categories::updateTree();
				if (Yii::app()->request->isAjaxRequest)
					Yii::app()->end();
				else
					$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->render('create', array( 'model' => $model));
	}

	public function actionUpdate($id) {
		$model = $this->loadModel($id, 'Categories');

		if (isset($_POST['Categories'])) {
			$model->attributes = $_POST['Categories'];
			$model->modified_by = Yii::app()->user->id;
			if($_POST['Categories']['modified']=='0000-00-00 00:00:00'){
				$model->modified = date('Y-m-d H:i:s');
			}
			if ($model->save()) {
				Categories::updateTree();
				$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->render('update', array(
				'model' => $model,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->request->isPostRequest) {
			$this->loadModel($id, 'Categories')->delete();

			if (!Yii::app()->request->isAjaxRequest)
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('Categories');
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin() {
		$model = new Categories('search');
		$model->unsetAttributes();
		if (isset($_GET['Categories']))
			$model->attributes = $_GET['Categories'];
		
		$this->render('admin', array(
			'model' => $model
		));
	}
	
}